package day52.org.controller;

import day52.basic.util.AjaxResult;
import day52.basic.util.PageList;
import day52.org.domain.Dictionarydetail;
import day52.org.query.DictionarydetailQuery;
import day52.org.service.IDictionarydetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

//Restful风格
//@GetMapping      简单查询
//@PostMapping     高级查询
//@PutMapping      新增更新数据
//@DeleteMapping   删除数据
//@PatchMapping    批量操作


@RestController
@RequestMapping("/dic")
@Api(tags = "字典细节接口")
public class DictionarydetailController {
    @Autowired
    private IDictionarydetailService dictionarydetailService;

    @ApiOperation(value = "字典细节查找",notes = "根据字典细节id查找")
    @GetMapping("/{id}")
    public Dictionarydetail findOne(@PathVariable Long id){
        return dictionarydetailService.findOne(id);
    }

    @ApiOperation(value = "字典细节查找",notes = "查找所有字典细节")
    @GetMapping
    public List<Dictionarydetail> findAll(){
       return dictionarydetailService.findAll();
    }

    @ApiOperation(value = "字典细节添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping
    public AjaxResult save(@RequestBody @Valid Dictionarydetail dictionarydetail){
        if(dictionarydetail.getId() == null){
            dictionarydetailService.add(dictionarydetail);
        }else{
            dictionarydetailService.update(dictionarydetail);
            System.out.println(1);
        }
        return AjaxResult.me();
    }

    @ApiOperation(value = "字典细节删除",notes = "根据字典细节id进行删除")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        dictionarydetailService.delete(id);
        return AjaxResult.me();
    }
    @ApiOperation(value = "高级查询",notes = "进行分页等高级查询")
    @PostMapping
    public PageList<Dictionarydetail> pageQuery(@RequestBody DictionarydetailQuery dictionarydetailQuery){
        return dictionarydetailService.pageQuery(dictionarydetailQuery);
    }

    @ApiOperation(value = "批量删除",notes = "根据字典细节id进行删除")
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        dictionarydetailService.patchDelete(ids);
        return AjaxResult.me();
    }

}
